package com.yhy.wxzn.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yhy.wxzn.common.enums.*;
import com.yhy.wxzn.common.utils.ObjectFilterHelper;
import com.yhy.wxzn.wms.entity.*;
import com.yhy.wxzn.wms.mapper.WmsPickDetailMapper;
import com.yhy.wxzn.wms.service.IWmsOutboundConfigService;
import com.yhy.wxzn.wms.service.IWmsPickDetailService;
import com.yhy.wxzn.wms.service.IWmsPickOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: 出库明细
 * @Author: jeecg-boot
 * @Date:   2023-11-08
 * @Version: V1.0
 */
@Service
@Transactional(rollbackFor = Exception.class)
@Slf4j
public class WmsPickDetailServiceImpl extends ServiceImpl<WmsPickDetailMapper, WmsPickDetail> implements IWmsPickDetailService {

    @Autowired
    private WmsPickDetailMapper pickDetailMapper;
    @Autowired
    private IWmsPickOrderService pickOrderService;
    @Autowired
    private IWmsOutboundConfigService outboundConfigService;

    @Override
    public IPage<WmsPickDetail> pageSelf(Page<WmsPickDetail> page, QueryWrapper<WmsPickDetail> queryWrapper) {
        return pickDetailMapper.pageSelf(page,queryWrapper);
    }

    @Override
    public void addPickDetail(WmsPickDetail wmsPickDetail) {
        ObjectFilterHelper.getObjDefault(wmsPickDetail);
        this.save(wmsPickDetail);
    }

    @Override
    public void refreshPickOrderStatus(String pkId) {
        WmsPickOrder pickOrder = pickOrderService.getById(pkId);
        //查找入库配置
        LambdaQueryWrapper<WmsOutboundConfig> configQuery = new LambdaQueryWrapper<>();
        configQuery.eq(WmsOutboundConfig::getStatus, ValidEnums.ENABLE.getValue());
        configQuery.eq(WmsOutboundConfig::getOrderType,pickOrder.getOrderType());
        WmsOutboundConfig outboundConfig = outboundConfigService.getOne(configQuery);
        LambdaQueryWrapper<WmsPickDetail> detailQuery = new LambdaQueryWrapper<>();
        detailQuery.eq(WmsPickDetail::getPickId,pkId);
        List<WmsPickDetail> details = this.list(detailQuery);
        if(!CollectionUtils.isEmpty(details)){
            refreshOrderDetails(details,outboundConfig);
            refreshGroupPick(pickOrder,details,outboundConfig);
        }
    }

    private void refreshGroupPick(WmsPickOrder pickOrder, List<WmsPickDetail> details, WmsOutboundConfig outboundConfig) {
        //按拣货状态分组
        Map<Integer,List<WmsPickDetail>> pickMap = details.stream().collect(Collectors.groupingBy(WmsPickDetail::getDetailStatus));
        if(!CollectionUtils.isEmpty(pickMap)){
            if(pickMap.size() > 1){
                pickOrder.setOrderStatus(WmsOrderStatusEnums.BFJH.getValue());
            }else{
                for (Integer detailStatus:pickMap.keySet()){
                    if(detailStatus.intValue() == WmsOrderStatusEnums.XJ.getValue()){
                        pickOrder.setOrderStatus(WmsOrderStatusEnums.XJ.getValue());
                    }else if(detailStatus.intValue() == WmsOrderStatusEnums.BFJH.getValue()){
                        pickOrder.setOrderStatus(WmsOrderStatusEnums.BFJH.getValue());
                    }else if(detailStatus.intValue() == WmsOrderStatusEnums.YJH.getValue()){
                        pickOrder.setOrderStatus(WmsOrderStatusEnums.JHWC.getValue());
                    }
                }
            }
        }
        //按分配状态分组
        Map<Integer,List<WmsPickDetail>> allocateMap = details.stream().collect(Collectors.groupingBy(WmsPickDetail::getAllocateStatus));
        if(!CollectionUtils.isEmpty(allocateMap)){
            if(allocateMap.size() > 1){
                pickOrder.setAllocateStatus(WmsOrderStatusEnums.BFFP.getValue());
            }else{
                for (Integer allocateStatus:allocateMap.keySet()){
                    if(allocateStatus.intValue() == WmsOrderStatusEnums.XJ.getValue()){
                        pickOrder.setAllocateStatus(WmsOrderStatusEnums.XJ.getValue());
                    }else if(allocateStatus.intValue() == WmsOrderStatusEnums.BFFP.getValue()){
                        pickOrder.setAllocateStatus(WmsOrderStatusEnums.BFFP.getValue());
                    }else if(allocateStatus.intValue() == WmsOrderStatusEnums.YFP.getValue()){
                        pickOrder.setAllocateStatus(WmsOrderStatusEnums.FPWC.getValue());
                    }
                }
            }
        }
        //按发货状态分组
        Map<Integer,List<WmsPickDetail>> shipMap = details.stream().collect(Collectors.groupingBy(WmsPickDetail::getShipStatus));
        if(!CollectionUtils.isEmpty(shipMap)){
            if(shipMap.size() > 1){
                pickOrder.setShipStatus(WmsOrderStatusEnums.BFFH.getValue());
            }else{
                for (Integer shipStatus:shipMap.keySet()){
                    if(shipStatus.intValue() == WmsOrderStatusEnums.XJ.getValue()){
                        pickOrder.setShipStatus(WmsOrderStatusEnums.XJ.getValue());
                    }else if(shipStatus.intValue() == WmsOrderStatusEnums.BFFH.getValue()){
                        pickOrder.setShipStatus(WmsOrderStatusEnums.BFFH.getValue());
                    }else if(shipStatus.intValue() == WmsOrderStatusEnums.YFH.getValue()){
                        pickOrder.setShipStatus(WmsOrderStatusEnums.FHWC.getValue());
                    }
                }
            }
        }
        //按过账状态分组
        if(!ObjectUtils.isEmpty(outboundConfig) && outboundConfig.getShipSycErp()){
            Map<Integer,List<WmsPickDetail>> synStatusMap = details.stream().collect(Collectors.groupingBy(WmsPickDetail::getSyncStatus));
            if(!CollectionUtils.isEmpty(synStatusMap)){
                if(synStatusMap.size() > 1){
                    pickOrder.setSyncStatus(WmsSyncStatusEnums.BFGZ.getValue());
                }else{
                    for (Integer syncStatus:synStatusMap.keySet()){
                        if(syncStatus.intValue() == WmsSyncStatusEnums.WGZ.getValue()){
                            pickOrder.setSyncStatus(WmsSyncStatusEnums.WGZ.getValue());
                        }else if(syncStatus.intValue() == WmsSyncStatusEnums.GZZ.getValue()){
                            pickOrder.setSyncStatus(WmsSyncStatusEnums.BFGZ.getValue());
                        }else if(syncStatus.intValue() == WmsSyncStatusEnums.GZCG.getValue()
                                || syncStatus.intValue() == WmsSyncStatusEnums.GZSB.getValue()){
                            pickOrder.setSyncStatus(WmsSyncStatusEnums.GZWC.getValue());
                        }
                    }
                }
            }
        }else{
            pickOrder.setSyncStatus(WmsSyncStatusEnums.WXGZ.getValue());
        }
        pickOrderService.saveOrUpdate(pickOrder);
    }

    private void refreshOrderDetails(List<WmsPickDetail> details, WmsOutboundConfig outboundConfig) {
        for (WmsPickDetail detail: details) {
            //需求数量
            BigDecimal demonQty = detail.getDemonQty();
            //已分配数量
            BigDecimal allocateQty = detail.getAllocateQty();
            //已拣货数量
            BigDecimal pickedQty = detail.getPickedQty();
            //已发货数量
            BigDecimal shipedQty = detail.getShipQty();
            //过账数量
            BigDecimal syncQty = detail.getSyncQty();
            //刷新分配数量
            if(allocateQty.compareTo(BigDecimal.ZERO) == 0){
                detail.setAllocateStatus(WmsOrderStatusEnums.XJ.getValue());
            }else if(allocateQty.compareTo(demonQty) == 0){
                detail.setAllocateStatus(WmsOrderStatusEnums.YFP.getValue());
            }else{
                detail.setAllocateStatus(WmsOrderStatusEnums.BFFP.getValue());
            }
            //刷新拣货数量
            if(pickedQty.compareTo(BigDecimal.ZERO) == 0){
                detail.setDetailStatus(WmsOrderStatusEnums.XJ.getValue());
            }else if(pickedQty.compareTo(demonQty) == 0){
                detail.setDetailStatus(WmsOrderStatusEnums.YJH.getValue());
            }else{
                detail.setDetailStatus(WmsOrderStatusEnums.BFJH.getValue());
            }
            //刷新发货数量
            if(shipedQty.compareTo(BigDecimal.ZERO) == 0){
                detail.setShipStatus(WmsOrderStatusEnums.XJ.getValue());
            }else if(shipedQty.compareTo(demonQty) == 0){
                detail.setShipStatus(WmsOrderStatusEnums.YFH.getValue());
            }else{
                detail.setShipStatus(WmsOrderStatusEnums.BFFH.getValue());
            }
            if(!ObjectUtils.isEmpty(outboundConfig) && outboundConfig.getShipSycErp()){
                if(syncQty.compareTo(BigDecimal.ZERO) == 0){
                    detail.setSyncStatus(WmsSyncStatusEnums.WGZ.getValue());
                }else if(syncQty.compareTo(demonQty) == 0){
                    detail.setSyncStatus(WmsSyncStatusEnums.GZCG.getValue());
                }else{
                    detail.setSyncStatus(WmsSyncStatusEnums.GZZ.getValue());
                }
            }else{
                detail.setSyncStatus(WmsSyncStatusEnums.WXGZ.getValue());
            }
        }
        this.saveOrUpdateBatch(details);
    }
}
